package feedback

import (
	"context"
	"math"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/feedback"
	"github.com/zeromicro/go-zero/core/logc"

	"github.com/zeromicro/go-zero/core/logx"
)

type FeedbackListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 意见反馈列表
func NewFeedbackListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FeedbackListLogic {
	return &FeedbackListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FeedbackListLogic) FeedbackList(req *types.FeedbackListReq) (resp *types.FeedbackListResData, err error) {
	resData := make([]types.FeedbackInfoData, 0)

	resp = &types.FeedbackListResData{
		Pagination: types.Pagination{
			PageSize:  int32(req.Size),
			Page:      int32(req.Page),
			Total:     0,
			TotalPage: 0,
		},
		Data: resData,
	}

	u, err := l.svcCtx.SettingService.Feedback.FeedbackList(l.ctx, &feedback.FeedbackListReq{Page: req.Page, Size: req.Size, UserId: req.UserId})

	if err != nil {
		logc.Error(l.ctx, "FeedbackList err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	for _, v := range u.Data.Data {
		resData = append(resData, types.FeedbackInfoData{
			Id:        v.Id,
			UserId:    v.UserId,
			Content:   v.Content,
			CreatedAt: v.CreatedAt,
		})
	}

	resp.Data = resData
	resp.Pagination.Total = u.Data.Count
	resp.Pagination.TotalPage = int64(math.Ceil(float64(resp.Pagination.Total) / float64(resp.Pagination.PageSize)))
	return resp, nil
}
